package com.example.practice;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.example.practice.bean.SensorValue;
import com.example.practice.creative.CreativeActivity;
import com.example.practice.db.SensorValueDB;
import com.example.practice.fragment.HistoryFragment;
import com.example.practice.fragment.RealTimeChartFragment;
import com.example.practice.fragment.RealTimeFragment;

public class MainActivity extends BaseActivity {
	
	
	private SharedPreferences sp;
	private SharedPreferences.Editor spe;
	private HistoryFragment historyFragment;
	private RealTimeFragment realTimeFragment;
	public RealTimeChartFragment realTimeChartFragment;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow();
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		historyFragment = new HistoryFragment();
		realTimeFragment = new RealTimeFragment();
		realTimeChartFragment = new RealTimeChartFragment();
		
		
		
		sp = getSharedPreferences("s", MODE_PRIVATE);
		if (sp.getBoolean("first", true)) {
			startActivity(new Intent(this, StudyActivity.class));
			spe = sp.edit();
			spe.putBoolean("first", false).commit();
		}

	
		getSupportFragmentManager()
		.beginTransaction().add(R.id.realtabcontent, realTimeFragment)
		.commit();
		
		findViewById(R.id.btn_realtime).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getSupportFragmentManager()
				.beginTransaction()

				.addToBackStack(null)
				.replace(R.id.realtabcontent, realTimeFragment)
				.commit();

			
			}
		});

		findViewById(R.id.btn_histroy).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getSupportFragmentManager()
				.beginTransaction().replace(R.id.realtabcontent, historyFragment)
				.commit();

			
			}
		});

		findViewById(R.id.btn_ctrl).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				startActivity(new Intent(MainActivity.this,
						ControllerActivity.class));
			}
		});

		findViewById(R.id.btn_settings).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Dialog dialog = new Dialog(arg0.getContext(),R.style.TransparentDialog);
				dialog.setTitle("透明对话框测试");
				TextView t = new TextView(arg0.getContext());
				t.setText("\n\n\n\n\n");
				dialog.setContentView(t);
				dialog.show();

			}
		});
		
		findViewById(R.id.btn_creative).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				startActivity(new Intent(MainActivity.this,
						CreativeActivity.class));
			}
		});
		
		
		findViewById(R.id.btn_languge).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				updateLanguge(Locale.ENGLISH);
			}
		});
		
	findViewById(R.id.btn_notify).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showNotification("sdfdf", 1);
			}
		});
	
		
	}

	//填写以下代码
	private void updateLanguge(Locale locale){
		Configuration configuration = getResources().getConfiguration();
		configuration.locale = locale;
		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
		getResources().updateConfiguration(configuration, displayMetrics);
		finish();
		Intent intent = new Intent(this,MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}
	
	private void saveData(SensorValue value){
		SensorValueDB sensorValueDB = new SensorValueDB(this);
		ArrayList<Object> list = new ArrayList<Object>();
		list.add(value);
		sensorValueDB.saveDataToDB(list, null);
	}
	
	@Override
	protected void onGetSensorValue(SensorValue sensorValue) {
		// TODO Auto-generated method stub
		saveData(sensorValue);

		if (realTimeFragment.isAdded()) {
			realTimeFragment.onGetSensorValue(sensorValue);
		}
		if(realTimeChartFragment.isAdded()){
			realTimeChartFragment.onGetSensorValue(sensorValue);
		}
		
		SensorValueDB sensorValueDB = new SensorValueDB(this);
		sensorValueDB.deleteSensorByTime(System.currentTimeMillis() - 600000);

	}
	
	//填写已下代码
	public void showNotification(String message,int id) {
		NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
		builder.setWhen(System.currentTimeMillis())
		.setDefaults(Notification.DEFAULT_ALL)
		.setSmallIcon(R.drawable.ic_launcher)
		.setContentText(message)
		.setAutoCancel(true)
		.setContentTitle(getString(R.string.app_name));
		
		TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
		taskStackBuilder.addNextIntent(new Intent(this,MainActivity.class));
		taskStackBuilder.addParentStack(this);
		
		PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
		builder.setContentIntent(pendingIntent);
		
		NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		manager.notify(id, builder.build());
		
	}

}
